//
//  ViewController.m
//  搜索框SearchBar
//
//  Created by 周成龙 on 17/5/15.
//  Copyright © 2017年 ZCL. All rights reserved.
//

#import "ViewController.h"
#import "SearchResultsController.h"

#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height
#define BarTintColor RGB(8, 42, 72)
#define RGBA(r, g, b, a)    [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r, g, b)     RGBA(r, g, b, 1.0f)

@interface ViewController ()<UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
{
    
    NSArray *_dataArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) SearchResultsController *resultsController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"首页";
    
    _dataArray = @[@"广州市",@"北京市",@"天津市",@"西安市",@"重庆市",@"沈阳市",@"青岛市",@"济南市",@"深圳市",@"长沙市",@"无锡市"];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screen_width, screen_height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    _tableView.sectionIndexColor = RGB(150, 150, 150);
    [self.view addSubview:_tableView];
    
    
    _resultsController = [[SearchResultsController alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsController];
    _searchController.delegate = self;
    _searchController.searchResultsUpdater=self;
    _searchController.searchBar.delegate = self;
    _tableView.tableHeaderView = self.searchController.searchBar;
}

#pragma mark - table view delegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 47;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identfire = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfire];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _dataArray[indexPath.row];
    return cell;
}

#pragma mark - UISearchBarDelegate
//修改SearchBar的Cancel Button 的Title
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [_searchController.searchBar setShowsCancelButton:YES animated:YES];
    UIButton *btn=[_searchController.searchBar valueForKey:@"_cancelButton"];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = [self.searchController.searchBar text];
    // 移除搜索结果数组的数据
//    [self.searchList removeAllObjects];
//    //过滤数据
//    self.searchList= [SearchResult getSearchResultBySearchText:searchString dataArray:self.allCityArray];
//    if (searchString.length==0&&self.searchList!= nil) {
//        [self.searchList removeAllObjects];
//    }
//    self.searchList = [self.searchList filterTheSameElement];
//    NSMutableArray *dataSource = nil;
//    if ([self.searchList count]>0) {
//        dataSource = [NSMutableArray array];
//        // 结局了数据重复的问题
//        for (NSString *str in self.searchList) {
//            [dataSource addObject:str];
//        }
//    }
    
    NSArray *titles = @[@"青岛市",@"济南市",@"深圳市",@"长沙市",@"无锡市"];
    
    //刷新表格
    self.resultsController.dataSource = titles;
    [self.resultsController.tableView reloadData];
    [_tableView reloadData];
    
}
/**
 *  点击了搜索的结果的 cell
 *
 *  @param resultVC  搜索结果的控制器
 *  @param follow    搜索结果信息的模型
 */
//- (void)resultViewController:(NBSearchResultController *)resultVC didSelectFollowCity:(NSString *)cityName
//{
//    self.searchController.searchBar.text =@"";
//    [self.searchController dismissViewControllerAnimated:NO completion:nil];
//    [self popRootViewControllerWithName:cityName];
//}

#pragma mark - UISearchControllerDelegate

@end
